<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="2px" class="demo-ruleForm">
    <el-form-item prop="name">
      <el-input v-model="ruleForm.name" prefix-icon="el-icon-user">
        <template slot="prepend">用户名</template>
      </el-input>
    </el-form-item>

    <el-form-item prop="pass">
      <el-input type="password" v-model="ruleForm.pass" auto-complete="off" prefix-icon="el-icon-more-outline">
        <template slot="prepend">密码</template>
      </el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off" prefix-icon="el-icon-more-outline">
        <template slot="prepend">确认密码</template>
      </el-input>
    </el-form-item>

    <br>
    <br>
    <el-form-item>
      <el-button type="primary" @click.stop="submitForm('ruleForm')">注册</el-button>
      <el-button @click.stop="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from "axios";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      activeName: 'second',
      ruleForm: {
        role: '',
        email: '',
        name: '',
        pass: '',
        checkPass: '',
        validateCode: '',
        code: false
      },
      rules: {
        name: [{required: true, message: '请输入您的名称', trigger: 'blur'}, {
          min: 2,
          max: 20,
          message: '长度在 2 到 20 个字符',
          trigger: 'blur'
        }],
        pass: [{required: true, validator: validatePass, trigger: 'blur'}],
        checkPass: [{required: true, validator: validatePass2, trigger: 'blur'}],
      }
    };
  },

  methods: {

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios({
            url: '/api/User/InsertUser',
            method: 'post',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'//普通表单提交的Content-Type
            },
            transformRequest: [data => {
              let formData = new FormData()
              formData.append("username", this.ruleForm.name)
              formData.append("password", this.ruleForm.pass)
              return formData
            }],
          }).then(res => {
            console.log(res)
            if (res.data === 1) {
              this.$message.success("注册成功")
              this.$router.push({path: '/index', query: {userId: this.ruleForm.name, role: "0"}});
            } else {
              this.$message.error("ddd")
            }
          }).catch(err => {
            console.log(err)
          })
        }

      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>